﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kopytko.BoardGame;
using Kopytko.GameBase;

namespace Kopytko.Games.Checkers
{
    public class CheckersLogic : BoardLogic
    {
        private BoardPlayer currentPlayer = null;

        public override void Init()
        {
            Pawns = new Pawns();
            Player1 = new BoardPlayer(0, PawnColor.Black, PlayerType.Human)
                {
                    Name = "Gracz 1",
                };
            Player2 = new BoardPlayer(1, PawnColor.White, PlayerType.Human)
                {
                    Name = "Gracz 2",
                };

            var player = Player1;

            

            //tworzenie listy pionków
            for (short i = 1; i < 64; i += 2)
            {
                if (i == 9) i = 8;
                if (i == 16) i = 17;
                if (i == 25)
                {
                    i = 40;
                    player = Player2;
                }
                if (i == 48) i = 49;
                if (i == 57) i = 56;
                Checker checker = new Checker(player as BoardPlayer, new BoardPosition(i));
                Pawns.Add(checker);
            }

            // gracz startowy.
            switchPlayers();
        }

        private void switchPlayers()
        {
            if (currentPlayer == Player1)
                currentPlayer = Player2;
            else
                currentPlayer = Player1;
        }

        public override IPlayer WhoTurnIs()
        {
            return currentPlayer;
        }

        public override IEnumerable<BoardPosition> GetAvailableMovements(BoardPosition currentPosition)
        {
            List<BoardPosition> positions = new List<BoardPosition>();
            var checker = Pawns.Where(p => p.X == currentPosition.X && p.Y == currentPosition.Y).SingleOrDefault() as Checker;

            if (checker != null && checker.Color == currentPlayer.Color)
            {
                if (checker.X>0 && checker.Y >0 )
                {
                    var pawn = getPawn(checker.X - 1, checker.Y - 1);
                    if (pawn==null)
                        positions.Add(new BoardPosition(checker.X-1, checker.Y-1));
                    else if (pawn.Color != currentPlayer.Color && getPawn(checker.X - 2, checker.Y - 2) == null && checker.X > 1 && checker.Y > 1)
                    {
                        positions.Add(new BoardPosition(checker.X - 2, checker.Y - 2));
                    }
                }

                if (checker.X > 0 && checker.Y < 7)
                {
                    var pawn = getPawn(checker.X - 1, checker.Y + 1);
                    if (pawn == null)
                        positions.Add(new BoardPosition(checker.X - 1, checker.Y + 1));
                    else if (pawn.Color != currentPlayer.Color && getPawn(checker.X - 2, checker.Y + 2) == null && checker.X > 1 && checker.Y < 6)
                    {
                        positions.Add(new BoardPosition(checker.X -2, checker.Y + 2));
                    }
                }

                if (checker.X < 7 && checker.Y < 7)
                {
                    var pawn = getPawn(checker.X + 1, checker.Y + 1);
                    if (pawn == null)
                        positions.Add(new BoardPosition(checker.X + 1, checker.Y + 1));
                    else if (pawn.Color != currentPlayer.Color && getPawn(checker.X + 2, checker.Y + 2) == null && checker.X < 6 && checker.Y < 6)
                    {
                        positions.Add(new BoardPosition(checker.X + 2, checker.Y + 2));
                    }
                }

                if (checker.X < 7 && checker.Y > 0)
                {
                    var pawn = getPawn(checker.X + 1, checker.Y - 1);
                    if (pawn == null)
                        positions.Add(new BoardPosition(checker.X + 1, checker.Y - 1));
                    else if (pawn.Color != currentPlayer.Color && getPawn(checker.X + 2, checker.Y - 2) == null && checker.X < 6 && checker.Y > 1)
                    {
                        positions.Add(new BoardPosition(checker.X + 2, checker.Y - 2));
                    }
                }
            }
            return positions;
        }

        public override bool IsMoveAllowed(BoardPosition currentPosition)
        {
            return GetAvailableMovements(currentPosition).Count() > 0;
        }

        public override MoveResult Move(BoardPosition currentPosition, BoardPosition destPosition)
        {
            var checker = getPawn(currentPosition.X, currentPosition.Y);

            Pawn beated = null;
            if (Math.Abs(currentPosition.X - destPosition.X) > 1)
            {
                var dirX = 1;
                var dirY = 1;

                if ((currentPosition.X - destPosition.X)<0)
                {
                    dirX = -1;
                }

                if ((currentPosition.Y - destPosition.Y) < 0)
                {
                    dirY = -1;
                }

                beated = getPawn(destPosition.X+dirX, destPosition.Y+dirY);
            }

            checker.X = destPosition.X;
            checker.Y = destPosition.Y;


            MoveResult result = new MoveResult()
            {
                State = Kopytko.BoardGame.GameState.Started,
            };

            if (beated != null)
            {
                beated.State = PawnState.Beated;
                result.BeatedPawns = new[]{beated};
                Pawns.Remove(beated);
            }


            switchPlayers();

            result.CurrentPlayer = currentPlayer;

            return result;
        }

        private Checker getPawn(int x, int y)
        {
            return Pawns.Where(p => p.X == x && p.Y == y).FirstOrDefault() as Checker;
        }
    }
}
